package com.ruan.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ruan.dao.UserMapper;
import com.ruan.entity.ResponseResult;
import com.ruan.entity.User;
import com.ruan.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Service;

import java.util.Date;

/**
 * @ClassName:UserServiceImpl
 * @Author:阮昇
 * @Description:
 * @Datetime: 2023/7/19 15:08
 **/
@Service
public class UserServiceImpl implements UserService {

    @Autowired
    private UserMapper userMapper;
    @Override
    public ResponseResult register(User user) {
        User user1 = userMapper.selectOne(new QueryWrapper<User>().eq("user_name", user.getUserName()));
        if (user1 != null){
            return new ResponseResult(100,"该用户名已被注册");
        }
        Date now = new Date();
        user.setCreateTime(now);
        user.setDelFlag(0);
        BCryptPasswordEncoder passwordEncoder = new BCryptPasswordEncoder();
        String encodedPassword = passwordEncoder.encode(user.getPassword());
        user.setPassword(encodedPassword);
        int row = userMapper.insert(user);
        if (row != 1){
            return new ResponseResult(100,"注册失败");
        }else {
            return new ResponseResult(200,"注册成功");
        }
    }
}
